package com.bafa.jdbc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class QueryObject {

	private LinkedHashMap fields = new LinkedHashMap(); 
	private ArrayList orderBy = new ArrayList();
	private String tableName = null;
	private ArrayList selectFields = new ArrayList();
	private boolean distinct;
	private String sumField = null;
	private LinkedHashMap orFields = new LinkedHashMap();
	private String queryString;
	public void setDistinct(boolean distinct){
		this.distinct=distinct;
	}
	
	
	public void addSelectFields(String fieldName){
		selectFields.add(fieldName);
	}
	
	public void addWhereAndField(String fieldName, String fieldValue){
		fields.put(fieldName,fieldValue);
	}
	
	public void addWhereOrField(String fieldName, String fieldValue){
		orFields.put(fieldName,fieldValue);
	}
	
	public void addOrderBy(String fieldName, boolean desc){
		String[] order = new String[2];
		
		order[0]=fieldName;
		if(desc)order[1]="desc";
		else order[1]="asc";
		
		orderBy.add(order);
	}
	
	public void queryString(){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append("select ");
		if(distinct)queryBuffer.append("distinct ");
		if(selectFields.size()==0)queryBuffer.append("* ");
		for(int i=0;i<selectFields.size();i++){
			if(i>0)queryBuffer.append(",");
			queryBuffer.append(((String)selectFields.get(i)));
		}
		
		queryBuffer.append(" from ");
		queryBuffer.append(tableName);
		String[] keys = (String[])fields.keySet().toArray(new String[fields.size()]);
		String[] values = (String[])fields.values().toArray(new String[fields.size()]);
		if(keys.length>0){
			queryBuffer.append(" where ");
		
			for(int i=0;i<keys.length;i++){
				if(i>0)queryBuffer.append(" AND ");
				queryBuffer.append(keys[i]+"="+values[i]);
			}
		}
		
		if(orderBy.size()>0)queryBuffer.append(" order by ");
		for(int i=0;i<orderBy.size();i++){
			if(i>0)queryBuffer.append(",");
			queryBuffer.append(((String[])orderBy.get(i))[0]);
			queryBuffer.append(" "+((String[])orderBy.get(i))[1]);
		}
		
		queryString = queryBuffer.toString();
	}
	
	
	public String countString(){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append("select count(*) from ");
		queryBuffer.append(tableName);
		String[] keys = (String[])fields.keySet().toArray(new String[fields.size()]);
		String[] values = (String[])fields.values().toArray(new String[fields.size()]);
		if(keys.length>0){
			queryBuffer.append(" where ");
		
		for(int i=0;i<keys.length;i++){
			if(i>0)queryBuffer.append(" AND ");
			queryBuffer.append(keys[i]+"="+values[i]);
			
		}
		}
		
		return queryBuffer.toString();
	}
	
	public QueryObject(String tableName){
		this.tableName = tableName;
	}

	
	public String getQueryString() {
		if(queryString==null||queryString.equalsIgnoreCase("")){
			queryString();
		}
		return queryString;
	}

	/**
	 * only use to override the query.......
	 * @return
	 */
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}


	public void setSumField(String sumField) {
		this.sumField = sumField;
	}
	
}
